/**
 * @class Extmx.Toaster
 * Preconfiguraci&oacte;n de la ventana. Implementa un efecto al cerrarla (ocultarla)<br><br>
 * 
 * @extends Ext.Window
 * @basado 	En el toaster de Edouard Fattal
 * @autor	Ing. Josue Hernandez Gomez<br>
 * 			joshua@extjs.mx<br>
 * 			http://www.joshua.com.mx
 * @fecha    11 de marzo de 2011.  M&eacute;xico DF
 */

Ext.namespace("Extmx.Toaster");

Extmx.Vector = {
    positions: []
};

Extmx.Toaster = Ext.extend(Ext.Window, {
    shadow :false,
    padding : "6px 8px 6px 8px",
    initComponent: function(){
        var config={
            cls: 'x-notification',
            dirShow:this.dirShow || 'b',
            dirHide:this.dirHide || 'b',
            posicion:this.posicion || 'br-br',
            width: 200,
            autoHeight: true,
            plain: false,
            draggable: false,
            bodyStyle: 'text-align:center'
        };
        
        Ext.apply(this,Ext.apply(this.initialConfig,config));

        if(this.autoDestroy) {
            this.task = new Ext.util.DelayedTask(this.hide, this);
        } else {
            this.closable = true;
        }
        Extmx.Toaster.superclass.initComponent.call(this);
    },
    setMessage: function(msg){
        this.body.update(msg);
    },
    setTitle: function(title, iconCls){
        Extmx.Toaster.superclass.setTitle.call(this, title, iconCls||this.iconCls);
    },
    onRender:function(ct, position) {
        Extmx.Toaster.superclass.onRender.call(this, ct, position);
    },
    onDestroy: function(){
        Extmx.Vector.positions.remove(this.pos);
        Extmx.Toaster.superclass.onDestroy.call(this);
    },
    cancelHiding: function(){
        this.addClass('fixed');
        if(this.autoDestroy) {
            this.task.cancel();
        }
    },
    afterShow: function(){
        Extmx.Toaster.superclass.afterShow.call(this);
        Ext.fly(this.body.dom).on('click', this.cancelHiding, this);
        if(this.autoDestroy) {
            this.task.delay(this.hideDelay || 5000);
        }
    },
    animShow: function(){
        this.pos = 0;
        while(Extmx.Vector.positions.indexOf(this.pos)>-1)
            this.pos++;
        Extmx.Vector.positions.push(this.pos);
        this.setSize(200,100);
        this.el.alignTo(document, this.posicion, [ -20, -35-((this.getSize().height+50)*this.pos) ]);
        this.el.slideIn(this.dirShow, {
            duration: 1,
            callback: this.afterShow,
            scope: this
        });
    },
    animHide: function(){
        Extmx.Vector.positions.remove(this.pos);
        this.el.ghost(this.dirHide, {
            duration: 1,
            remove: true
        });
    },

    focus: Ext.emptyFn
});